package com.dem.javatestwebsocket.controller;

import com.dem.javatestwebsocket.model.ChatHistory;
import com.dem.javatestwebsocket.service.IOnlineuser;
import com.dem.javatestwebsocket.service.impl.ChatHistoryServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("OnlineUser")
public class OnlineController {
    @Autowired
    private IOnlineuser iOnlineuser;
    @Autowired
    private ChatHistoryServiceImpl chatHistoryService;


    @PostMapping("/Long")
    @ApiOperation("用户登录")
    public String userLogin(@RequestParam String userId) {
        iOnlineuser.userLogin(userId);
        return "User " + userId + " logged in successfully.";
    }
    @PostMapping("/logout")
    public String userLogout(@RequestParam String userId) {
        iOnlineuser.userLogout(userId);
        return "User " + userId + " logged out successfully.";
    }

    @GetMapping("/online-users")
    public Set<String> getOnlineUsers() {
        return iOnlineuser.getOnlineUsers();
    }
    @PostMapping("history")
    public String addMessage(@RequestBody ChatHistory chatHistory) {
        chatHistoryService.add(chatHistory);
        return "Message saved successfully.";
    }


}
